package com.mxpio.erp.plan.controller;

import com.mxpio.erp.common.enums.OrderStatusEnums;
import com.mxpio.erp.common.plan.entity.MpsVersionTemp;
import com.mxpio.erp.common.plan.service.MpsVersionTempService;
import com.mxpio.erp.common.technology.vo.AuditOpinionVO;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.List;

@Slf4j
@Tag(name = "MpsVersionTempController", description = "主计划版本temp接口")
@RestController
@RequestMapping("/erp/plan/mpstemp/")
public class MpsVersionTempController {

	@Autowired
	private MpsVersionTempService mpsVersionTempService;


	@GetMapping("list")
	@Operation(summary = "主计划版本temp列表", description = "获取主计划版本temp列表（无分页）", method = "GET")
	public Result<List<MpsVersionTemp>> list(@Parameter(description = "查询参数") Criteria criteria)
			throws UnsupportedEncodingException {
		List<MpsVersionTemp> mpsVersion = mpsVersionTempService.list(MpsVersionTemp.class, criteria);
		return Result.OK(mpsVersion);
	}
	
	@GetMapping("page")
	@Operation(summary = "主计划版本temp列表", description = "获取主计划版本temp列表（分页）", method = "GET")
	public Result<Page<MpsVersionTemp>> page(@Parameter(description = "查询参数") Criteria criteria,
			@Parameter(description = "每页条数") Integer pageSize, @Parameter(description = "页号") Integer pageNo)
			throws UnsupportedEncodingException {
		Pageable page = PageRequest.of(pageNo - 1, pageSize);
		Page<MpsVersionTemp> mpsVersion = mpsVersionTempService.listPage(MpsVersionTemp.class, page, criteria);
		return Result.OK(mpsVersion);
	}

	/*@GetMapping("list/{id}")
	@Operation(summary = "根据id获取主计划版本temp", description = "根据id获取主计划版本temp", method = "GET")
	public Result<MpsVersionTemp> getById(
			@Parameter(description = "ID") @PathVariable(name = "id", required = true) String id) {
		MpsVersionTemp mpsVersion = mpsVersionTempService.getById(MpsVersionTemp.class, id);
		return Result.OK(mpsVersion);
	}

	@DeleteMapping("remove/{ids}")
	@Operation(summary = "根据ids删除主计划版本temp", description = "删除主计划版本temp", method = "DELETE")
	@Transactional(readOnly = false)
	public Result<MpsVersionTemp> remove(
			@Parameter(description = "id，多个单号用英文逗号分隔") @PathVariable(name = "ids", required = true) String ids) {
		String[] id = ids.split(",");
		for (String key : id) {
			mpsVersionTempService.delete(MpsVersionTemp.class, key);
		}
		return Result.OK();
	}*/
	
	/*@PutMapping("submit/{id}")
    @Operation(summary = "提交主计划", description = "提交主计划", method = "PUT")
    public Result<MpsVersionTemp> submit(@Parameter(description = "ID") @PathVariable(name = "id", required = true) String id) {
		MpsVersionTemp mpsVersopm = mpsVersionTempService.getById(MpsVersionTemp.class, id);
        if (mpsVersopm.getBpmnStatus().equals(OrderStatusEnums.BpmnStatus.CREATE.getStatusCode())) {
            return mpsVersionTempService.updateBpmnStatus(mpsVersopm, OrderStatusEnums.BpmnStatus.AUDITING);
        } else {
            return Result.error("非开立状态不可提交");
        }
    }
	*/
	@PutMapping("audit/{id}")
    @Operation(summary = "审核主计划版本temp", description = "审核主计划版本temp", method = "PUT")
    public Result<MpsVersionTemp> audit(@Parameter(description = "ID") @PathVariable(name = "id", required = true) String id,@RequestBody AuditOpinionVO auditOpinionVO) {
		MpsVersionTemp mpsVersopm = mpsVersionTempService.getById(MpsVersionTemp.class, id);
        if (mpsVersopm.getBpmnStatus().equals(OrderStatusEnums.BpmnStatus.AUDITING.getStatusCode())) {
			return mpsVersionTempService.auditWithOpinion(mpsVersopm, auditOpinionVO);
        } else {
            return Result.error("非审核中状态不可审核");
        }
    }

   /* @PutMapping("abandon/{id}")
    @Operation(summary = "弃审主计划", description = "弃审主计划", method = "PUT")
    public Result<MpsVersionTemp> abandon(@Parameter(description = "ID") @PathVariable(name = "id", required = true) String id) {
    	MpsVersionTemp mpsVersopm = mpsVersionTempService.getById(MpsVersionTemp.class, id);
        if (mpsVersopm.getBpmnStatus().equals(OrderStatusEnums.BpmnStatus.AUDITING.getStatusCode()) || mpsVersopm.getBpmnStatus().equals(OrderStatusEnums.BpmnStatus.AUDITED.getStatusCode())) {
            return mpsVersionTempService.updateBpmnStatus(mpsVersopm, OrderStatusEnums.BpmnStatus.CREATE);
        } else {
            return Result.error("非审核中、已审核状态不可审核");
        }
    }*/

}
